<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="iccid/卡号" prop="iccid">
        <el-input
          v-model="queryParams.iccid"
          placeholder="请输入iccid/卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="卡片状态" prop="cardsStatus">
        <el-select v-model="queryParams.cardsStatus" placeholder="请选择卡片状态" clearable>
          <el-option
            v-for="dict in dict.type.card_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:cards:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:cards:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:cards:remove']"
        >删除</el-button>
      </el-col>
    <!--  <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:cards:export']"
        >导出</el-button>
      </el-col>
-->


      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleBangDing"
          v-hasPermi="['system:cards:edit']"
        >绑定卡</el-button>
      </el-col>


      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cardsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="ICCID"  width="200"  align="center" prop="iccid" />
      <el-table-column label="卡号" align="center" prop="cardNumber" />
      <el-table-column label="运营商" align="center" prop="operator" />
      <el-table-column label="卡片状态" align="center" prop="cardsStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.card_status" :value="scope.row.cardsStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="实名认证" align="center" prop="realName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_rz" :value="scope.row.realName"/>
        </template>
      </el-table-column>
      <el-table-column label="流量用量" align="center" prop="consumption" />
      <el-table-column label="已购套餐" align="center" prop="packageName" />
      <el-table-column label="累充金额" align="center" prop="totalMoney" />
      <el-table-column label="开卡时间" align="center" prop="cardOpenTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cardOpenTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:cards:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:cards:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改卡片管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="iccid" prop="iccid" >
          <el-input v-model="form.iccid" placeholder="请输入iccid" />
        </el-form-item>
        <el-form-item label="卡号" prop="cardNumber">
          <el-input v-model="form.cardNumber" placeholder="请输入卡号" />
        </el-form-item>
        <el-form-item label="imei" prop="imei">
          <el-input v-model="form.imei" placeholder="请输入imei" />
        </el-form-item>
        <el-form-item label="运营商" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入运营商" />
        </el-form-item>
        <el-form-item label="卡片状态" prop="cardsStatus">
          <el-radio-group v-model="form.cardsStatus">
            <el-radio
              v-for="dict in dict.type.card_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="实名认证，0表示未认证，1表示已认证" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入实名认证，0表示未认证，1表示已认证" />
        </el-form-item>
        <el-form-item label="流量用量" prop="consumption">
          <el-input v-model="form.consumption" placeholder="请输入流量用量" />
        </el-form-item>
        <el-form-item label="已购套餐" prop="packageName">
          <el-input v-model="form.packageName" placeholder="请输入已购套餐" />
        </el-form-item>
        <el-form-item label="累充金额" prop="totalMoney">
          <el-input v-model="form.totalMoney" placeholder="请输入累充金额" />
        </el-form-item>
        <el-form-item label="开卡时间" prop="cardOpenTime">
          <el-date-picker clearable
            v-model="form.cardOpenTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开卡时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="关联设备" prop="equipment">
          <el-input v-model="form.equipment" placeholder="请输入关联设备" />
        </el-form-item>
        <el-form-item label="负责人" prop="name">
          <el-input v-model="form.name" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="经销商名称" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入经销商名称" />
        </el-form-item>
        <el-form-item label="所属经销商,0表示未归属经销商" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入所属经销商,0表示未归属经销商" />
        </el-form-item>
        <el-form-item label="所属客户，0表示未有用户" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入所属客户，0表示未有用户" />
        </el-form-item>
        <el-form-item label="所属流量池,0表示未进入流量池" prop="poolId">
          <el-input v-model="form.poolId" placeholder="请输入所属流量池,0表示未进入流量池" />
        </el-form-item>
        <el-form-item label="销售额" prop="salePrice">
          <el-input v-model="form.salePrice" placeholder="请输入销售额" />
        </el-form-item>
        <el-form-item label="佣金" prop="commission">
          <el-input v-model="form.commission" placeholder="请输入佣金" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="上次停机时间" prop="shutdownTime">
          <el-date-picker clearable
            v-model="form.shutdownTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上次停机时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


<!--  解绑/绑定卡  -->
    <select-package ref="select" :cards="ids" @ok="handleQuery" />

  </div>
</template>

<script>
import { listCards, getCards, delCards, addCards, updateCards,queryPackege } from "@/api/system/cards";
import selectPackage from "./selectPackage";
export default {
  name: "Cards",
  dicts: ['card_status','is_rz'],
  components: { selectPackage },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 卡片管理表格数据
      cardsList: [],
      // 弹出层标题
      title: "",
      titleBangDing:"",
      // 是否显示弹出层
      open: false,
      openBangDing:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        iccid: null,
        cardNumber: null,
        imei: null,
        operator: null,
        cardsStatus: null,
        realName: null,
        realTime: null,
        consumption: null,
        packageName: null,
        totalMoney: null,
        cardOpenTime: null,
        equipment: null,
        name: null,
        businessName: null,
        userId: null,
        customerId: null,
        poolId: null,
        assignmentStatus: null,
        salePrice: null,
        commission: null,
        remarks: null,
        shutdownTime: null,
        packages:[]
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询卡片管理列表 */
    getList() {
      this.loading = true;
      listCards(this.queryParams).then(response => {
        this.cardsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        iccid: null,
        cardNumber: null,
        imei: null,
        operator: null,
        cardsStatus: null,
        realName: null,
        realTime: null,
        consumption: null,
        packageName: null,
        totalMoney: null,
        cardOpenTime: null,
        equipment: null,
        name: null,
        businessName: null,
        userId: null,
        customerId: null,
        poolId: null,
        assignmentStatus: null,
        salePrice: null,
        commission: null,
        remarks: null,
        shutdownTime: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加卡片管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCards(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改卡片管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCards(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCards(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除卡片管理编号为"' + ids + '"的数据项？').then(function() {
        return delCards(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/cards/export', {
        ...this.queryParams
      }, `cards_${new Date().getTime()}.xlsx`)
    },
    /** 打开授权用户表弹窗 */
    handleBangDing(row){
      const ids = row.id || this.ids;
      this.$refs.select.show();
    }
  }
};
</script>
